<?php
/* $Id: statuses.admin.php,v 1.5 2003/10/08 07:11:12 tuxmonkey Exp $ */
/**
 * @package Issue-Tracker
 * @subpackage Administration
 */

if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.".basename(__FILE__);exit;}
  

if(is_admin() or permission_check('status_manager')){

	$langu = $_SESSION['language'];
	if ($langu) $langu = 'EN';
			
	switch($_GET['subaction'])
	{
		case  "new" :
			if ($_POST['commit'])
			{

				 $sql  = "SELECT ttask FROM tasks ";
        		 $sql .= "WHERE LOWER(ttask) = LOWER('".trim($_POST['ttask'])."')";
       			 $tid = $dbi->fetch_one($sql);
				 if (!empty($tid)) 
					push_error($lg->err_al_create );
				 else
				 {
					$insert['ttask'] = $_POST['ttask'];
         			$dbi->insert("tasks",$insert);
					unset($insert);
					$insert['ttask'] = $_POST['ttask'];
					$insert['langu'] = $langu;
					$insert['vtext'] = $_POST['vtext'];					
         			$dbi->insert("taskst",$insert);
					unset($insert);					
         			redirect("?module=admin&action=tasks");
					break;
				}	
			}

			$smarty->assign('title',$lg->create.$lg->task);
	 		$smarty->display("admin/tasks/new.tpl");
			break;
		
		case  "edit" :
			if ($_POST['commit'])
			{
			  $update['vtext'] = $_POST['vtext'];
          	  $dbi->update("taskst",$update,"WHERE ttask ='".$_GET['id']."' AND langu = '$langu'");
			  unset($update);			  
			  $update['stats'] = $_POST['stats'];
          	  $dbi->update("tasks",$update,"WHERE ttask ='".$_GET['id']."'");
			  redirect("?module=admin&action=tasks");
		      break;
			}
			if (!empty($_GET['id']))
			{
				$sql  = "SELECT h.ttask,h.stats,h.loekz,t.vtext ";
			    $sql .= "FROM tasks h, taskst t ";
			    $sql .= "WHERE h.ttask = t.ttask ";
			    $sql .= "AND h.ttask = '".$_GET['id']."' AND t.langu = '$langu' ";
				
				$s_datas = $dbi->fetch_all($sql,'array');
				$smarty->assign('title',$lg->edit.$lg->task);
				$smarty->assign('s_datas',$s_datas[0]);				
				$smarty->display("admin/tasks/edit.tpl");
				break;
			}

			redirect("?module=admin&action=tasks");			
			break;
		case  "activate" :
			if (!empty($_GET['id']))
			{  
       	  	  $update['loekz'] = ' ';
          	  $dbi->update("tasks",$update,"WHERE ttask ='".$_GET['id']."'");
			  redirect("?module=admin&action=tasks");
			  break;			  			
			}		
		case  "delete" :
			if (!empty($_GET['id']))
			{
       	  	  $update['loekz'] = 'X';
          	  $dbi->update("tasks",$update,"WHERE ttask ='".$_GET['id']."'");
			  redirect("?module=admin&action=tasks");
		      break;		
			}
			

									
		default:
    		$links[] = array( "txt" => $lg->create.$lg->task,
         					  "url" => "?module=admin&action=tasks&subaction=new",
      						  "img" => $_ENV['imgs']['status']);
							  
			if($_GET['deleted'] == "true")				  
    		{	$links[] = array( "txt" => $lg->mm_hide_deleted,
         						  "url" => "?module=admin&action=tasks&deleted=false",
      							  "img" => $_ENV['imgs']['status']);
					$sql_loekz = "";		  
			}				  
			else
			{	$links[] = array( "txt" => $lg->mm_show_deleted,
         						  "url" => "?module=admin&action=tasks&deleted=true",
      							  "img" => $_ENV['imgs']['status']);
					$sql_loekz = "AND loekz = ' ' ";	  
			}				  
							  							  
			// Edition des taches
			$sql  = "SELECT h.ttask,t.vtext,h.stats,h.loekz ";
			$sql .= "FROM tasks h, taskst t ";
			$sql .= "WHERE h.ttask = t.ttask AND t.langu = '$langu' ";
			$sql .= $sql_loekz;		
			$sql .= "ORDER BY h.ttask";
			$t_datas = $dbi->fetch_all($sql,"array");
			if(is_array($t_datas))
			{
				foreach($t_datas as $key => $s_datas)
				{
					$loekz = $s_datas['loekz'];
					unset($s_datas['loekz']);
					$s_edit = $s_datas;
					$s_edit['iedit'] = "?module=admin&action=tasks&subaction=edit&id=".$s_datas['ttask'] ;
					if(empty($loekz))
					{
						$ilink = "?module=admin&action=tasks&subaction=delete&id=".$s_datas['ttask'] ;	
						$srcnm = "idele" ;
					}
					else
					{
						$ilink = "?module=admin&action=tasks&subaction=activate&id=".$s_datas['ttask'] ;
						$srcnm = "active" ;
					}
					$s_edit['idele'] = array('ilink' => $ilink,'srcnm' => $srcnm );
					$t_edit[] = $s_edit;
				}
			}
			$o_grid = new Grid();
			$o_grid->setFieldCat(array('ttask','vtext','statu','iedit','idele'));
			$o_grid->setDatas($t_edit);
			$t_tolbr[] = new ObjBr('1',GMsg('00',2,null,$lg->task),'left');
			$o_grid->setToolBar($t_tolbr);
			// mise en forme
			$o_grid->setFieldAttr("statu","oconv", false); 
			// Affichage
			$smarty->display("title_bar.tpl");			
			 echo $o_grid->Display();			
	}
	
} else {
  access_denied();    
}
?>